import { formatPrice } from '../../utils/util'

function colorSelect(currId, currSpecId, skuList, sizeList, payFlag, platform_activity_sku_list) {
  let skuPrice = ''
  let skuOriginPrice = ''
  let skuImg = ''
  let skuSpec = ''
  let skuId = ''
  let tureSpec = []
  let result = {}

  for (var i = 0, len = skuList.length; i < len; i++) {
    if (skuList[i].goods_color_id == currId) {
      // 获取当前颜色可匹配的规格
      tureSpec.push(skuList[i].goods_size_id)
    }

    if ((skuList[i].goods_size_id == currSpecId || skuList[i].goods_size_name == "") && skuList[i].goods_color_id == currId) {
      if (payFlag) {
        skuPrice = skuList[i].goods_single_price_calculate
      } else {
        skuPrice = skuList[i].goods_price_calculate
      }

      skuImg = skuList[i].image_list[0].new_path
      skuSpec = skuList[i].goods_spec
      skuId = skuList[i].sku_id
    }
  }

  // 活动的商品，若无库存，则显示原价
  const curSizeId = skuList.find(item => item.goods_color_id == currId && (item.goods_size_id == currSpecId || !currSpecId));
  if(platform_activity_sku_list && curSizeId) {
    const activity = platform_activity_sku_list.find(item => item.sku_id == curSizeId.sku_id);
  
    if (activity && !Number(activity.activity_stock)) {
        skuPrice = formatPrice(curSizeId.goods_price / 100);
        skuOriginPrice = '';
    } else {
        skuOriginPrice = (currSpecId || !sizeList.length) ? formatPrice(activity.market_price / 100) : '';
    }
  }

  for (var i = 0, len = sizeList.length; i < len; i++) {
    if (tureSpec.indexOf(sizeList[i].size_id) >= 0) {
      sizeList[i].noStock = false
    } else {
      sizeList[i].noStock = true
    }
  }

  result = {
    skuImg,
    skuPrice,
    skuOriginPrice,
    skuSpec,
    skuId,
  }

  return result
}

function specSelect(currId, currColorId, skuList, colorList, payFlag, platform_activity_sku_list) {
  let skuPrice = ''
  let skuOriginPrice = ''
  let skuImg = ''
  let skuSpec = ''
  let skuId = ''
  let trueColor = []
  let result = {}

  for (var i = 0, len = skuList.length; i < len; i++) {
    if (skuList[i].goods_size_id == currId) {
      // 获取当前规格可匹配的颜色
      trueColor.push(skuList[i].goods_color_id)
    }

    if (skuList[i].goods_size_id == currId && skuList[i].goods_color_id == currColorId) {
      if (payFlag) {
        skuPrice = skuList[i].goods_single_price_calculate
      } else {
        skuPrice = skuList[i].goods_price_calculate
      }
      skuImg = skuList[i].image_list[0].new_path
      skuSpec = skuList[i].goods_spec
      skuId = skuList[i].sku_id
    }
  }

  // 活动的商品，若无库存，则显示原价
  const curSizeId = skuList.find(item => (item.goods_color_id == currColorId || !currColorId) && item.goods_size_id == currId);
  if(platform_activity_sku_list && curSizeId) {
    const activity = platform_activity_sku_list.find(item => item.sku_id == curSizeId.sku_id);
  
    if (activity && !Number(activity.activity_stock)) {
      skuPrice = formatPrice(curSizeId.goods_price / 100);
      skuOriginPrice = '';
    } else {
        skuOriginPrice = (currColorId || !colorList.length) ? formatPrice(activity.market_price / 100) : '';
    }
  }

  for (var i = 0, len = colorList.length; i < len; i++) {
    if (trueColor.indexOf(colorList[i].color_id) >= 0) {
      colorList[i].noStock = false
    } else {
      colorList[i].noStock = true
    }
  }

  result = {
    skuImg: skuImg,
    skuPrice: skuPrice,
    skuOriginPrice,
    skuSpec: skuSpec,
    skuId: skuId,
  }

  return result
}

export { colorSelect, specSelect }